<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .task-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>内容管理弹窗页面设置</span>
    </div>
    <div class="line"></div>
    <!-- <task-list-form
      class="task-form"
      @on-success-valid="handleSubmit">
    </task-list-form> -->
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加弹窗页面
    </Button>
    <content-setting-modal
      @edit-content-setting-valid="handleEdit">
    </content-setting-modal>
    <confirm-modal
      @confirm-valid="handleConfirm">
    </confirm-modal>
    <delete-modal
      @delete-valid="confirmDelete">
    </delete-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TaskListForm from '_c/task-list-form'
import ContentSettingModal from '_c/content-setting-modal'
import ConfirmModal from '_c/confirm-modal'
import DeleteModal from '_c/delete-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    TaskListForm,
    ContentSettingModal,
    ConfirmModal,
    DeleteModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '内容管理弹窗页面设置',
      confirmData: {},
      index: '',
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '创建日期',
          key: 'ctime',
          // width: 200,
          align: 'center'
        },
        {
          title: '名称',
          key: 'name',
          // width: 200,
          align: 'center'
        },
        {
          title: '简单描述',
          key: 'content',
          // width: 250,
          align: 'center'
        },
        {
          title: '开始时间',
          key: 'btime',
          // width: 220,
          align: 'center'
        },
        {
          title: '结束时间',
          key: 'etime',
          // width: 226,
          align: 'center'
        },
        {
          title: '状态',
          key: 'state',
          align: 'center',
          width: 200,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === 1 ? '启动' : params.row.state === 0 ? '关闭' : '-'
            )
          }
        },
        {
          title: '操作',
          key: 'cz',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '编辑'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleDelete(params.index, params.row)
                  }
                }
              }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      contentModalList: state => state.contentModal.contentModalList,
      dataCount: state => state.contentModal.contentModalDataCount,
      historyData: state => state.contentModal.contentModalHistoryData,
      pageSize: state => state.contentModal.contentModalPageSize
    })
  },
  methods: {
    ...mapActions([
      'getContentModalList',
      'getContentModalCurrentRow',
      'editContentModalData',
      'updateModalState',
      'updateConfirmModalState',
      'updateDeleteModalState',
      'changeContentModalPage',
      'changeContentModalPageSize',
      'deleteData'
    ]),
    handleSubmit (playload) {
      this.getContentModalList(playload)
    },
    handleClick (id, row) {
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getContentModalCurrentRow(newRow)
      this.updateModalState(true)
    },
    handleDelete (index) {
      // console.log(index)
      this.index = index
      this.updateDeleteModalState(true)
    },
    confirmDelete () {
      this.updateDeleteModalState(false)
      this.contentModalList.splice(this.index, 1)
      this.changePage(this.page)
    },
    handleEdit (playload, id) {
      // console.log(playload)
      this.confirmData = playload
      this.updateModalState(false)
      this.updateConfirmModalState(true)
    },
    handleConfirm () {
      // this.editContentModalData(this.confirmData).then(
      //   this.getContentModalList({})
      // )
      this.updateConfirmModalState(false)
    },
    changePage (index) {
      this.page = index
      this.changeContentModalPage(index)
    },
    changePageSize (value) {
      this.changeContentModalPageSize(value)
      this.changePage(this.page)
    }
  },
  created () {
    this.getContentModalList({})
  }
}
</script>
